home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / mimetools.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  8KB  |  255 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Various tools used by MIME-reading or MIME-writing programs.'''
  5. import os
  6. import sys
  7. import tempfile
  8. from warnings import filterwarnings, catch_warnings
  9. with catch_warnings():
  10.     if sys.py3kwarning:
  11.         filterwarnings('ignore', '.*rfc822 has been removed', DeprecationWarning)
  12.     import rfc822
  13. from warnings import warnpy3k
  14. warnpy3k('in 3.x, mimetools has been removed in favor of the email package', stacklevel = 2)
  15. __all__ = [
  16.     'Message',
  17.     'choose_boundary',
  18.     'encode',
  19.     'decode',
  20.     'copyliteral',
  21.     'copybinary']
  22.  
  23. class Message(rfc822.Message):
  24.     '''A derived class of rfc822.Message that knows about MIME headers and
  25.     contains some hooks for decoding encoded and multipart messages.'''
  26.     
  27.     def __init__(self, fp, seekable = 1):
  28.         rfc822.Message.__init__(self, fp, seekable)
  29.         self.encodingheader = self.getheader('content-transfer-encoding')
  30.         self.typeheader = self.getheader('content-type')
  31.         self.parsetype()
  32.         self.parseplist()
  33.  
  34.     
  35.     def parsetype(self):
  36.         str = self.typeheader
  37.         if str is None:
  38.             str = 'text/plain'
  39.         if ';' in str:
  40.             i = str.index(';')
  41.             self.plisttext = str[i:]
  42.             str = str[:i]
  43.         else:
  44.             self.plisttext = ''
  45.         fields = str.split('/')
  46.         for i in range(len(fields)):
  47.             fields[i] = fields[i].strip().lower()
  48.         
  49.         self.type = '/'.join(fields)
  50.         self.maintype = fields[0]
  51.         self.subtype = '/'.join(fields[1:])
  52.  
  53.     
  54.     def parseplist(self):
  55.         str = self.plisttext
  56.         self.plist = []
  57.         while str[:1] == ';':
  58.             str = str[1:]
  59.             if ';' in str:
  60.                 end = str.index(';')
  61.             else:
  62.                 end = len(str)
  63.             f = str[:end]
  64.             if '=' in f:
  65.                 i = f.index('=')
  66.                 f = f[:i].strip().lower() + '=' + f[i + 1:].strip()
  67.             self.plist.append(f.strip())
  68.             str = str[end:]
  69.  
  70.     
  71.     def getplist(self):
  72.         return self.plist
  73.  
  74.     
  75.     def getparam(self, name):
  76.         name = name.lower() + '='
  77.         n = len(name)
  78.         for p in self.plist:
  79.             if p[:n] == name:
  80.                 return rfc822.unquote(p[n:])
  81.         
  82.  
  83.     
  84.     def getparamnames(self):
  85.         result = []
  86.         for p in self.plist:
  87.             i = p.find('=')
  88.             if i >= 0:
  89.                 result.append(p[:i].lower())
  90.                 continue
  91.         return result
  92.  
  93.     
  94.     def getencoding(self):
  95.         if self.encodingheader is None:
  96.             return '7bit'
  97.         return None.encodingheader.lower()
  98.  
  99.     
  100.     def gettype(self):
  101.         return self.type
  102.  
  103.     
  104.     def getmaintype(self):
  105.         return self.maintype
  106.  
  107.     
  108.     def getsubtype(self):
  109.         return self.subtype
  110.  
  111.  
  112.  
  113. try:
  114.     import thread
  115. except ImportError:
  116.     import dummy_thread as thread
  117.  
  118. _counter_lock = thread.allocate_lock()
  119. del thread
  120. _counter = 0
  121.  
  122. def _get_next_counter():
  123.     global _counter
  124.     _counter_lock.acquire()
  125.     _counter += 1
  126.     result = _counter
  127.     _counter_lock.release()
  128.     return result
  129.  
  130. _prefix = None
  131.  
  132. def choose_boundary():
  133.     """Return a string usable as a multipart boundary.
  134.  
  135.     The string chosen is unique within a single program run, and
  136.     incorporates the user id (if available), process id (if available),
  137.     and current time.  So it's very unlikely the returned string appears
  138.     in message text, but there's no guarantee.
  139.  
  140.     The boundary contains dots so you have to quote it in the header."""
  141.     global _prefix
  142.     import time as time
  143.     if _prefix is None:
  144.         import socket as socket
  145.         
  146.         try:
  147.             hostid = socket.gethostbyname(socket.gethostname())
  148.         except socket.gaierror:
  149.             hostid = '127.0.0.1'
  150.  
  151.         
  152.         try:
  153.             uid = repr(os.getuid())
  154.         except AttributeError:
  155.             uid = '1'
  156.  
  157.         
  158.         try:
  159.             pid = repr(os.getpid())
  160.         except AttributeError:
  161.             pid = '1'
  162.  
  163.         _prefix = hostid + '.' + uid + '.' + pid
  164.     return '%s.%.3f.%d' % (_prefix, time.time(), _get_next_counter())
  165.  
  166.  
  167. def decode(input, output, encoding):
  168.     '''Decode common content-transfer-encodings (base64, quopri, uuencode).'''
  169.     if encoding == 'base64':
  170.         import base64
  171.         return base64.decode(input, output)
  172.     if None == 'quoted-printable':
  173.         import quopri as quopri
  174.         return quopri.decode(input, output)
  175.     if None in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
  176.         import uu as uu
  177.         return uu.decode(input, output)
  178.     if None in ('7bit', '8bit'):
  179.         return output.write(input.read())
  180.     if None in decodetab:
  181.         pipethrough(input, decodetab[encoding], output)
  182.     else:
  183.         raise ValueError, 'unknown Content-Transfer-Encoding: %s' % encoding
  184.  
  185.  
  186. def encode(input, output, encoding):
  187.     '''Encode common content-transfer-encodings (base64, quopri, uuencode).'''
  188.     if encoding == 'base64':
  189.         import base64
  190.         return base64.encode(input, output)
  191.     if None == 'quoted-printable':
  192.         import quopri
  193.         return quopri.encode(input, output, 0)
  194.     if None in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
  195.         import uu
  196.         return uu.encode(input, output)
  197.     if None in ('7bit', '8bit'):
  198.         return output.write(input.read())
  199.     if None in encodetab:
  200.         pipethrough(input, encodetab[encoding], output)
  201.     else:
  202.         raise ValueError, 'unknown Content-Transfer-Encoding: %s' % encoding
  203.  
  204. uudecode_pipe = '(\nTEMP=/tmp/@uu.$$\nsed "s%^begin [0-7][0-7]* .*%begin 600 $TEMP%" | uudecode\ncat $TEMP\nrm $TEMP\n)'
  205. decodetab = {
  206.     'uuencode': uudecode_pipe,
  207.     'x-uuencode': uudecode_pipe,
  208.     'uue': uudecode_pipe,
  209.     'x-uue': uudecode_pipe,
  210.     'quoted-printable': 'mmencode -u -q',
  211.     'base64': 'mmencode -u -b' }
  212. encodetab = {
  213.     'x-uuencode': 'uuencode tempfile',
  214.     'uuencode': 'uuencode tempfile',
  215.     'x-uue': 'uuencode tempfile',
  216.     'uue': 'uuencode tempfile',
  217.     'quoted-printable': 'mmencode -q',
  218.     'base64': 'mmencode -b' }
  219.  
  220. def pipeto(input, command):
  221.     pipe = os.popen(command, 'w')
  222.     copyliteral(input, pipe)
  223.     pipe.close()
  224.  
  225.  
  226. def pipethrough(input, command, output):
  227.     (fd, tempname) = tempfile.mkstemp()
  228.     temp = os.fdopen(fd, 'w')
  229.     copyliteral(input, temp)
  230.     temp.close()
  231.     pipe = os.popen(command + ' <' + tempname, 'r')
  232.     copybinary(pipe, output)
  233.     pipe.close()
  234.     os.unlink(tempname)
  235.  
  236.  
  237. def copyliteral(input, output):
  238.     while None:
  239.         line = input.readline()
  240.         if not line:
  241.             break
  242.         continue
  243.         return None
  244.  
  245.  
  246. def copybinary(input, output):
  247.     BUFSIZE = 8192
  248.     while None:
  249.         line = input.read(BUFSIZE)
  250.         if not line:
  251.             break
  252.         continue
  253.         return None
  254.  
  255.